package com.xuexiang.yoyo.utils;


import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CDKeyEncryptUtils {
    //--------------AES---------------
    private static final String KEY = "32055296";  // 密匙，必须16位
    private static final String OFFSET = "32055296"; // 偏移量
    private static final String ENCODING = "UTF-8"; // 编码
    private static final String ALGORITHM = "DES"; //算法
    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; // 默认的加密算法，CBC模式


    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String AESencrypt(String data) throws Exception {
        //指定算法、获取Cipher对象(DES/CBC/PKCS5Padding：算法为，工作模式，填充模式)
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        //根据自定义的加密密匙和算法模式初始化密钥规范
        SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM);
        //CBC模式偏移量IV
        IvParameterSpec iv = new IvParameterSpec(OFFSET.getBytes());
        //初始化加密模式
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        //单部分加密结束，重置Cipher
        byte[] encrypted = cipher.doFinal(data.getBytes(ENCODING));
        //加密后再使用BASE64做转码
        return Base64.getEncoder().encodeToString(encrypted);
//        return Base64().encodeToString(encrypted);
    }


}